package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.pojo.controlspec.AutoCompleteTextViewSpec;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class AutoCompleteTextViewFormat {
	private static final String COMPLETION_THRESHOLD = "completionThreshold";
	private static final String CONTENT_DATA = "contentdata";
	private static final String DEFAULT_VALUE = "defaultValue";
	private static final String DISPLAYPROP = "displayprop";
	private static final String FIELD = "field";
	private static final String NAME = "name";
	private static final String ROW_ITEM = "rowitem";
	private static final String RPTR_DESIGN = "rptrdesign";
	private static final String SHOW_ALL_BUTTON = "showAllButton";
	private static final String TOUCH_TO_SHOW_DROP_DOWN = "touchToShowDropDown";
	private static final String VALUEPROP = "valueprop";

	public static AutoCompleteTextViewSpec parse(final Node node) {
		AutoCompleteTextViewSpec autoCompleteTextViewSpec = new AutoCompleteTextViewSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil
			        .getAttrValue(node, COMPLETION_THRESHOLD);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setCompletionThreshold(Integer
				        .parseInt(value));
			}

			value = XmlPaserUtil.getAttrValue(node, CONTENT_DATA);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setContentData(value);
			}

			value = XmlPaserUtil.getAttrValue(node, DEFAULT_VALUE);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setDefaultValue(value);
			}

			value = XmlPaserUtil.getAttrValue(node, VALUEPROP);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setValueProperty(value);
			}

			value = XmlPaserUtil.getAttrValue(node, DISPLAYPROP);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setDisplayProperty(value);
			}

			value = XmlPaserUtil.getAttrValue(node, TOUCH_TO_SHOW_DROP_DOWN);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("Y")) {
					autoCompleteTextViewSpec.setTouchToShowDropDown(true);
				}
			}

			value = XmlPaserUtil.getAttrValue(node, SHOW_ALL_BUTTON);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setShowAllButton(value);
			}

			value = XmlPaserUtil.getAttrValue(node, RPTR_DESIGN);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setRprtDesign(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ROW_ITEM);
			if (!TextUtils.isEmpty(value)) {
				autoCompleteTextViewSpec.setRowItem(value);
			}
		}

		final NodeList nodeList = ((Element) node).getElementsByTagName(FIELD);
		Node field = null;
		String value = null;
		for (int i = 0; i < nodeList.getLength(); i++) {
			field = nodeList.item(i);
			if (field.hasAttributes()) {
				value = XmlPaserUtil.getAttrValue(field, NAME);
				if (!TextUtils.isEmpty(value)) {
					autoCompleteTextViewSpec.getSearchFields().add(value);
				}
			}
		}

		return autoCompleteTextViewSpec;
	}
}